package org.lanqiao.dxzf.controller.fee;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.lanqiao.dxzf.entity.Fee;
import org.lanqiao.dxzf.entity.Paging;
import org.lanqiao.dxzf.service.fee.FeeService;
import org.lanqiao.dxzf.util.StringCut;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FeeController {
	@Resource
	FeeService feeService;
	
	/*@RequestMapping("/showFee")	//访问时记得加.do  11
	public String showFee() {
		return "fee/fee_list";
	}*/
	
	/**
	 * 跳转到资费管理主页面
	 * @param paging
	 * @param request
	 * @return
	 */
	@RequestMapping("/fee_list")
	public String  fee_list(Paging paging,HttpServletRequest request){
		//ModelAndView mav = new ModelAndView();
		List<Fee> listFee = feeService.selectFee();
		if(listFee==null){
			/*request.setAttribute("user", user);//回显数据
			request.setAttribute("errorMsg", "用户名或密码错误");*/
			System.out.println("listFee为空");
			return null;
		}else{
			//刚开始的页面为第一页
		    if (paging.getCurrentPage() == null){
		    	paging.setCurrentPage(1);
		    } else {
		    	paging.setCurrentPage(paging.getCurrentPage());
		    }
		    //设置每页数据为十条
		    paging.setPageSize(6);
		    //每页的开始数
		    paging.setStar((paging.getCurrentPage() - 1) * paging.getPageSize());
		    //list的大小
		    int count = listFee.size();
		    //设置总页数
		    paging.setTotalPage(count % 6 == 0 ? count / 6 : count / 6 + 1);
		    //对list进行截取
		    paging.setDataList(listFee.subList(paging.getStar(),count-paging.getStar()>paging.getPageSize()?paging.getStar()+paging.getPageSize():count));
		    //设置作用域
		    request.setAttribute("paging", paging);               
		    return "fee/fee_list";
			/*HttpSession session=request.getSession();
			session.setAttribute("listFee", listFee);
			//mav.addObject("listFee", listFee);
			mav.setViewName("fee/fee_list");
			return mav;*/
		}
	}
	/**
	 * 删除资费信息
	 * @param fee
	 * @return
	 */
	@RequestMapping("/fee_delete")
	public String fee_delete(Fee fee,HttpServletRequest request) {
		int i = feeService.fee_delete(fee);
		if(i>0) {
			request.setAttribute("update", "yes");
			return "forward:fee_list.do";
		}else {
			request.setAttribute("update", "no");
			return "forward:fee_list.do";
		}
	}
	/**
	 * 资费启用
	 * @param fee
	 * @param request
	 * @return
	 */
	@RequestMapping("/fee_use")
	public String fee_use(Fee fee,HttpServletRequest request) {
		fee.setFstatus("启用");
		int i = feeService.fee_use(fee);
		if(i>0) {
			request.setAttribute("update", "yes");
			return "forward:fee_list.do";
		}else {
			request.setAttribute("update", "no");
			return "forward:fee_list.do";
		}
	}
	/**
	 * 查看资费详细信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/fee_detail")
	public String fee_detail(HttpServletRequest request) {
		int fid = Integer.valueOf(request.getParameter("fid"));
		/*String fname = request.getParameter("fname");
		String fstatus = request.getParameter("fstatus");
		String fkind = request.getParameter("fkind");
		String basic_time = request.getParameter("basic_time");
		String basic_price = request.getParameter("basic_price");
		String unit_price = request.getParameter("unit_price");
		String f_create_time = request.getParameter("f_create_time");
		String f_start_time = request.getParameter("f_start_time");
		String f_explain = request.getParameter("f_explain");*/
		Fee fee= new Fee();
		fee.setFid(fid);
		fee = feeService.fee_detail(fee);
		StringCut stringCut = new StringCut();
		fee.setBasic_time(stringCut.stringCut(fee.getBasic_time(), 2));
		fee.setBasic_price(stringCut.stringCut(fee.getBasic_price(), 1));
		fee.setUnit_price(stringCut.stringCut(fee.getUnit_price(), 4));
		request.setAttribute("fee", fee);
		return "fee/fee_detail";
	}
	/**
	 * 资费修改页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/fee_modi")
	public String fee_modi(HttpServletRequest request) {
		int fid = Integer.valueOf(request.getParameter("fid"));
		Fee fee= new Fee();
		fee.setFid(fid);
		fee = feeService.fee_detail(fee);
		StringCut stringCut = new StringCut();
		fee.setBasic_time(stringCut.stringCut(fee.getBasic_time(), 2));
		fee.setBasic_price(stringCut.stringCut(fee.getBasic_price(), 1));
		fee.setUnit_price(stringCut.stringCut(fee.getUnit_price(), 4));
		request.setAttribute("fee", fee);
		System.out.println();
		return "fee/fee_modi";
	}
	/**
	 * 资费修改提交
	 * @param fee
	 * @param request
	 * @return
	 */
	@RequestMapping("/fee_modi_alter")
	public String fee_modi_alter(Fee fee,HttpServletRequest request) {
		int i = feeService.fee_modi_alter(fee);
		request.setAttribute("fee",fee);
		if(i>0) {
			request.setAttribute("update", "yes");
			return "fee/fee_modi";
		}else {
			request.setAttribute("update", "no");
			return "fee/fee_modi";
		}
	}
	/**
	 * 跳转到资费添加页面
	 * @param fee
	 * @param request
	 * @return
	 */
	@RequestMapping("/fee_add_show")
	public String fee_add_show() {
		return "fee/fee_add";
	}
	/**
	 * 提交添加的资费
	 * @param fee
	 * @param request
	 * @return
	 */
	@RequestMapping("/fee_add")
	public String fee_add(Fee fee,HttpServletRequest request) {
		SimpleDateFormat myFmt1 = new SimpleDateFormat("yyyy-MM-dd"); //获取系统时间     "yyyy-MM-dd  hh:mm:ss" 时间格式，加上时分秒
		Date now = new Date();//获取系统时间
		String f_create_time = myFmt1.format(now);//获取系统时间
		fee.setF_create_time(f_create_time);
		int i = feeService.fee_add(fee);
		request.setAttribute("fee",fee);
		if(i>0) {
			request.setAttribute("update", "yes");
			return "fee/fee_add";
		}else {
			request.setAttribute("update", "no");
			return "fee/fee_add";
		}
	}
	
}
